define(["dojo/_base/declare","dijit/_WidgetBase", "dijit/_TemplatedMixin", 
        "dojo/text!./paginationWidget/templates/paginationWidget.html",       
        "dojo", "dojo/_base/xhr", "dojo/on", "dojo/dom", "dojo/dom-class", "dojo/parser",
		"dojo/query", "dojo/dom-attr", "dojo/dom-style", "dojo/dom-construct", "dojo/keys",
		"dojox/widget/Standby", "dojo/fx/Toggler", "dojo/fx", "dojox/fx/text", "dojox/html", "dojox/validate", "dojo/_base/connect", 
		"dijit/registry", "dojo/_base/array", "dojo/_base/lang", "dijit/layout/ContentPane",
		"dojox/html/_base",  "dojo/NodeList-traverse", "dojo/NodeList-manipulate"],
    function(declare, WidgetBase, TemplatedMixin, template, dojo, xhr, on, dom, domClass, parser, query, domAttr, domStyle, construct, keys,
			Standby, Toggler, fx, text, html, validate, connect, registry, array, lang, ContentPane){		
        return declare("dojoCustom.paginationWidget", [WidgetBase, TemplatedMixin], {
        	items: [],
        	pages: [],
            templateString: template,
            currentPageIndex: 0,
            baseClass: "paginationWidget",
        	postCreate: function(){
        	    var domNode = this.domNode;
        	    console.log(domNode);
        	    this.inherited(arguments);
        	    var domNode = this.domNode;
        	    this.inherited(arguments);
        	    var profileSize = this.items.length;
        	    var pageNumbers = Math.floor(profileSize/6);
        	    if(profileSize%6 > 0){
        	    	pageNumbers++;
        	    }
        	    var newDiv = construct.create("a", {class : "", innerHTML: "PREV", href: "javascript:void(0)", 'pageIndex' : "prev"   });
        	    this.connect(newDiv, "onclick", "switchPage");
        	    construct.place(newDiv, this.pageNumberDivNode);
        	    counter = 0;
        	    for (var i=0;i<pageNumbers;i++){
        	    	var newDiv = construct.create("a", {class : "pageNumber", innerHTML: i+1, href: "javascript:void(0)", 'pageIndex' : i  });
        	    	if(i == 0) domClass.add(newDiv, "active");
            	    construct.place(newDiv, this.pageNumberDivNode);
            	    this.connect(newDiv, "onclick", "switchPage");
            	    var temp = [];
            	    for (var n=0;n<6;n++){
            	    	temp[n] = this.items[counter++];
            	    	if(counter == this.items.length){
            	    		break;
            	    	}
            	    }
            	    this.pages[i] = temp;
            	    
        	    }    
        	    var newDiv = construct.create("a", {class : "", innerHTML: "NEXT", href: "javascript:void(0)", 'pageIndex' : "next"   });
        	    this.connect(newDiv, "onclick", "switchPage");
        	    construct.place(newDiv, this.pageNumberDivNode);
        	    var profContainer2 = construct.toDom("<div id='tempProfileContainer'></div>");
				html.set(this.profileWidgetsContainerNode, profContainer2);
        	    array.forEach(this.pages[0], function(profileWidget, index){
            		profileWidget.placeAt(profContainer2);
		        }); 
        	},
        	switchPage : function(e){
        		var pageIndexSelected = domAttr.get(query(e.currentTarget)[0], "pageIndex");
        		      		
        		
        		if(pageIndexSelected.indexOf("prev") != -1){
        			if(currentPageIndex == 0) return;
        			currentPageIndex--;
        		} else if(pageIndexSelected.indexOf("next") != -1 ){
        			if(currentPageIndex == this.pages.length-1) return;
        			currentPageIndex++;
        		} else{
        			currentPageIndex = parseInt(pageIndexSelected);        			
        		}
        		query(".pageNumber").forEach(function(node){
        			domClass.remove(node, "active");
        		  });  
        		query(".pageNumber").forEach(function(node){
        			nodePageAttr = domAttr.get(node, "pageIndex");
        			if(parseInt(nodePageAttr) == currentPageIndex)
        				domClass.add(node, "active");
        		  });  
        		html.set(this.profileWidgetsContainerNode, "");
        		var profContainer2 = construct.toDom("<div id='tempProfileContainer'></div>");
				html.set(this.profileWidgetsContainerNode, profContainer2);
        		array.forEach(this.pages[currentPageIndex], function(profileWidget, index){
        			profileWidget.placeAt(profContainer2);
		        });
        	}
        });
});
